#include <bits/stdc++.h>
using namespace std;
int a[200005];
int tong[1000005];
struct node{
	int num,h;
}sum[200005];

bool cmp(node a,node b){
	return a.num > b.num;
}

int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int t;
	cin >> t;
	while (t--){
		int n;
		cin >> n;
		int maxn = -1,minn = 1000001;
		for (int i = 1;i <= n;i++){
			scanf("%d",&a[i]);
			tong[a[i]]++;
			maxn = max(maxn,a[i]);
			minn = min(minn,a[i]);
		}
		int len = 0;
		for (int i = 1;i <= n;i++){
			if (!a[i]){
				sum[++len].num = a[i] * tong[a[i]];
				sum[len].h = a[i];
				a[i] = 0;
			}
			
		}
		sort(sum + 1 , sum + 1 + n,cmp);
		long long ans = 0;
		int last;
		for (int i = 1;i <= n;i++){
			if (a[i] == sum[1].h){
				ans += a[i];
				tong[a[i]]--;
				if (tong[a[i]] == 0){
					sort(sum + 1 , sum + 1 + n,cmp);
				}
			}else{
				if (last == a[i]) ans += a[i];
				last = a[i];
				tong[a[i]]--;
			}
			for (int j = 1;j <= len;j++){
				 if (sum[j].h == a[i]){
				 	sum[j].num -= a[i];
				 }
			}
		}
		cout << ans << endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}